#include "simplechatserver.h"
#include <QBuffer>
#include <QTcpSocket>

SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
{
        connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}

SimpleChatServer::~SimpleChatServer()
{
}

void SimpleChatServer::addConnection()
{
        QTcpSocket* connection = nextPendingConnection();
        connections.append(connection);
        QBuffer* buffer = new QBuffer(this);
        buffer->open(QIODevice::ReadWrite);
        buffers.insert(connection, buffer);
        connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
        connect(connection, SIGNAL(readyRead()),	SLOT(receiveMessage()));
        qDebug() << "connected\n";
}

void SimpleChatServer::removeConnection()
{
        QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
        QBuffer* buffer = buffers.take(socket);
        buffer->close();
        buffer->deleteLater();
        connections.removeAll(socket);
        socket->deleteLater();
        qDebug() << "removed con\n";
}

void SimpleChatServer::receiveMessage()
{
        QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
        QBuffer* buffer = buffers.value(socket);

        // missing some checks for returns values for the sake of simplicity
        qint64 bytes = buffer->write(socket->readAll());
        // go back as many bytes as we just wrote so that it can be read
        buffer->seek(buffer->pos() - bytes);
        // read only full lines, line by line

        qDebug() << "recived\n";

        int count = 0;
        while (buffer->canReadLine())
        {
            count++;
                QByteArray line = buffer->readLine();
                foreach (QTcpSocket* connection, connections)
                {
                        connection->write(line);
                }
                qDebug() << count  << " " << line << " recived\n";
        }

}
